package org.upt.dcda;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.upt.dcda.db.BookEntity;
import org.upt.dcda.db.BookTagEntity;
import org.upt.dcda.db.TagEntity;
import org.upt.dcda.exceptions.BookstoreException;

/**
 * Session Bean implementation class BookTagManagerBean
 */
@Stateless
@LocalBean
public class BookTagManagerBean implements BookTagManagerBeanRemote,
		BookTagManagerBeanLocal
{

	@PersistenceContext
	private EntityManager	entityManager;

	/**
	 * Default constructor.
	 */
	public BookTagManagerBean()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public void addBookTag(BookEntity book, TagEntity tag)
			throws BookstoreException
	{
		try
		{
			BookTagEntity booktag = new BookTagEntity();

			booktag.setBook(book);
			booktag.setTag(tag);

			entityManager.persist(booktag);
		}
		catch (Throwable t)
		{
			throw new BookstoreException("Caught exception: " + t.toString(), t);
		}

	}

}
